/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_STREAMBUF_H
#define GENERIK_STREAMBUF_H

#include <stdlib.h>

#include <streambuf>

/** \file streambuf.h
This file partially derives a generik streambuf class.
This streambuf defines a get- and a put-buffer of data of a specific size,
and provides functionality to correctly set up the begin, next and end
pointers for both buffers.
It simply serves to implement a buffered stream. The specialisation
of the underflow(), overflow() and pbackfail() methods are left for
specialised classes which know how to retrieve and send data to and from a
specific device.

Good overview of the details of the streambuf class: http://adtmag.com/joop/carticle.aspx?ID=300
Article - looks interesting: http://www.mr-edd.co.uk/?p=16
*/

namespace generik
{



	/** basic generik stream buffer
	Defines two buffers for input and output, and methods with which to
	set up the access pointers to them. Manages the buffer memory by
	automatically assigning and freeing heap-space for it.
	*/
	template <typename _CharT, typename _Traits, std::size_t _Size = 12>
	struct basic_streambuf
	: public std::basic_streambuf<_CharT, _Traits>
	{
		typedef _CharT		char_type;
		typedef _Traits		traits_type;
		typedef typename traits_type::int_type	int_type;
		
		typedef std::basic_streambuf<char_type, traits_type>	__streambuf_type;
		
		inline const std::size_t __buffer_size() const { return _Size; };
		char_type __gbuffer[_Size];
		char_type __pbuffer[_Size];
		
		/** constructor
		Initialises the streambuf with both buffers equal in the specified size.
		This resets the access pointers to their default positions.
		*/
		basic_streambuf()
		{
			g_reset();
			p_reset();
		}

		/** destructor
		*/
		~basic_streambuf()
		{}
		
		/** resets the get access pointers to describe the buffer as filled with offset number of items
		The next read will be able to get up to that amount of items before underflow is called again.
		*/
		void g_reset(std::size_t offset)
		{
			__streambuf_type::setg(&__gbuffer[0], &__gbuffer[0], &__gbuffer[offset]);
		}
		
		void g_reset()
		{
			__streambuf_type::setg(&__gbuffer[0], &__gbuffer[0], &__gbuffer[0]);
		}
		
		void p_reset()
		{
			__streambuf_type::setp(&__pbuffer[0], &__pbuffer[_Size]);
		}

	};



}

#endif

